/**
 * Created by jackbean on 17/3/2.
 */


var cur_env = "test";
$(function () {
    var userId = $.cookie('userId');
    //首页在最外层,其它页面在文件夹中,因此需要控制logo的路径。
    var headerUrl = "",modalUrl = "",imgUrl = "";
    if(location.pathname == "/index.html" || location.pathname.indexOf("/trunk/index.html")>-1){
        headerUrl = "template/header.html";
        modalUrl = "template/modifyPwdModal.html";
        imgUrl = "images/mj.jpg";
    }else{
        headerUrl = "../template/header.html";
        modalUrl = "../template/modifyPwdModal.html";
        imgUrl = "../images/mj.jpg";
    }

    //加载公共头部和菜单标签
    $.get(headerUrl,function (html) {
        $(document.body).prepend(html);
        //头部用户信息赋值
        $("#userId").text(userId);
        //头部logo赋值
        $(".leftnav img").attr("src",imgUrl);
        //初始化头部修改密码事件
        initHeader();
    });

    if (userId) {
        // $.get("/data/menu.json", "",function (data) {
        $.postRequest(majiangurl + "getMenus?userId=" + userId, "", function (data) {
            //如果status==0,表示接口返回成功
            if (data.status == '200') {
                //获取接口返回的菜单数据
                var menusList = data.result.menus;
                if (menusList && menusList.length > 0) {
                    var ul = $(".leftnav ul");
                    renderMenu(menusList, ul);
                    setCheckMenuColor();
                    // setMenuHeight();
                }
            } else {
                common.lightBox.showMessage(data.message);
            }
          /*  $(".nav-menu>a").on('click', function () {
                $(this).next().slideToggle();

                $(this).closest(".nav-menu").siblings(".nav-menu").find(".sub-menu").each(function(){
                    if($(this).is(":visible")){
                        $(this).slideUp()
                    }
                });
            });*/

        });
    }
    //动态添加左侧菜单栏内容
    function renderMenu(menuList, parent) {
        $.each(menuList, function (i, menu) {
            var href =  pageurl+menu.url;
            parent.append('<li id="' + menu.menuid + '" class="nav-menu"><a href="' + href + '" ><span class="glyphicon glyphicon-align-justify" aria-hidden="true"></span>' + menu.menuname + '</a></li>');
        });
    }
    //左侧菜单栏选中加颜色
    function setCheckMenuColor() {
        $("li a").click(function () {
            if ($(this).attr("href").indexOf("javascript") == -1) {
                $(".nav-menu a").removeClass("check");
                $(this).addClass("check");
                $(".leftnav").css('display',"none");
                $(".container-fluid").css('marginLeft',"0");;
            }
        });
    }
    //设置菜单的高度
    // function setMenuHeight() {
    //     $(".leftnav").css("height", $(window).height() + "px");
    //     $(".container-fluid,.right").css("height", ($(window).height()-40) + "px");
    //     //$(".right").css("width", ($(window).width() - 200) + "px");
    // }

    //初始化头部事件信息
    function initHeader() {
        var flag = false;//判断原密码是否输入正确
        //修改密码
        $("#userModifyPwd").on("click",function () {
            $("#Modify input[type=password]").val("");
            $("#errorInfo,#newInfo,#errorPwd").empty("");
            if($("#userModifyPwdModal").length==0){
                $.get(modalUrl,function (modal) {
                    $(document.body).append(modal);
                    $("#userModifyPwdModal").modal("show");
                    modifyInit();
                });
            }else{
                $("#userModifyPwdModal").modal("show");
            }
        });

        //用户退出
        $("#userLogout").on("click",function () {
            $.postRequest(majiangurl+"logout",'', function(data){});
            document.location.href=pageurl;
        });
       

        function modifyInit() {
            // alert($.cookie('userId'))
            $("#userName").text($.cookie('userId'));

            $("#Modify input").on("focus",function(){
                $(this).css("border-color","#75d77b");
            });
            $("#Modify input").on("blur",function(){
                $(this).css("border-color","");
            });

            //修改密码成功后,直接退出
            $("#modifySuccess").on("click",function () {
                $("#userLogout").trigger("click");
            });
            var localstorage=window.localStorage;
            //修改密码提交
            $("#submit_button").on("click",function () {
                var password = $("#oldPwd").val();
                var newpwd = $("#newPwd").val();
                var againpwd = $("#againPwd").val();
                if(password == ""){
                    $("#errorInfo").text("原始密码不能为空");
                    return false;
                }else{
                    $("#errorInfo").text("");
                }
                $.postRequest(majiangurl+"checkPassword",{"password":password},
                    function(data){
                        localstorage.sta = data.status
                    });

                if(localstorage.sta!='200'){
                    alert("st"+localstorage.sta);
                    $("#errorInfo").text("原始密码错误,请重新输入");
                    return false;
                }else{
                    $("#errorInfo").text("");
                }
                if(newpwd == "" ){
                    $("#newInfo").text("新密码不能为空");
                    return false;
                }else{
                    $("#newInfo").text("");
                }
                // if(!/^[^\u4E00-\u9FA5|\s]{6,20}$/g.test(newpwd)){
                //     $("#newInfo").text("新密码由6-20位非中文和空格组成");
                if(newpwd.length<6){
                    $("#newInfo").text("密码长度至少六位");
                    return false;
                }else{
                    $("#newInfo").text("");
                }

                if(password==newpwd){
                    $("#newInfo").text("新密码与原密码不一致");
                    return false;
                }else{
                    $("#newInfo").text("");
                }
                if(againpwd == "" ){
                    $("#errorPwd").text("请再次输入密码");
                    return false;
                }else{
                    $("#errorPwd").text("")
                }
                if(againpwd != newpwd ){
                    $("#errorPwd").text("两次密码不一致");
                    return false;
                }else{
                    $("#errorPwd").text("");
                }
                if(flag==false){
                    $("#errorInfo").text("原始密码输入错误");
                    return false;
                }
                var oldPwd = $("#oldPwd").val();
                var newPwd = $("#newPwd").val();
                var againPwd = $("#againPwd").val();
                $.postRequest(majiangurl+"updatePassword",{"str": JSON.stringify ({
                    "oldPass":oldPwd,
                    "newPass":newPwd,
                    "newPass2":againPwd
                })},function(data){
                    if(data.status =='200'){
                        $("#userModifySuccessModal").modal("show");
                        $("#userModifyPwdModal").modal("hide");
                    }else {
                        $("#errorPwd").text("修改密码失败,请重试!");
                    }
                });
            });

            //原始密码鼠标移出事件
            $("#oldPwd").on("blur",function () {
                var userId = $.cookie('userId');
                var password = $("#oldPwd").val();
                if(password !=""){
                    $("#errorInfo").text("");
                    $.postRequest(majiangurl+"checkPassword",{"userId":userId,"password":password},
                        function(data){
                            if(data.status !='200'){
                                $("#errorInfo").text("原始密码错误,请重新输入");
                                flag = false;
                            }else{
                                $("#errorInfo").text("");
                                flag = true;
                            }
                        });
                }else if(password ==""){
                    $("#errorInfo").text("原始密码不能为空");
                }
            });
            //新密码鼠标移出事件
            $("#newPwd").on("blur",function(){
                var password = $("#passWord").val();
                var newpwd = $("#newPwd").val();
                if(newpwd == "" ){
                    $("#newInfo").text("新密码不能为空");
                    return false;
                }else{
                    $("#newInfo").text("");
                }
                // if(!/^[^\u4E00-\u9FA5|\s]{6,20}$/g.test(newpwd)){ //更改密码规则
                if(!/^(?=.*[0-9].*)(?=.*[A-Z].*)(?=.*[a-z].*).{6,20}$/g.test(newpwd)){
                    $("#newInfo").text("新密码由6-20位且必须包含大写字母,小写字母和数字组成");
                    return false;
                }else{
                    $("#newInfo").text("");
                }
                if(password==newpwd){
                    $("#newInfo").text("新密码与原密码不能一致");
                }else{
                    $("#newInfo").text("");
                }
            });
            //再次输入密码鼠标移出事件
            $("#againPwd").on("blur",function(){
                var againpwd = $("#againPwd").val();
                var newpwd = $("#newPwd").val();
                if(againpwd == "" ){
                    $("#errorPwd").text("请再次输入密码");
                    return false;
                }else{
                    $("#errorPwd").text("")
                }
                if(againpwd != newpwd ){
                    $("#errorPwd").text("两次密码不一致");
                    return false;
                }else{
                    $("#errorPwd").text("");
                }
            });
        }
    }
});